import math
from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from com.atzl.ui.DataBaseUI import Ui_MainWindow as database
from com.atzl.common.Win import Windows
from com.atzl.functions.accounting.func.UpdatePrice import UpdatePrice
from com.atzl.functions.accounting.func.SetPrice import SetPrice
from com.atzl.common.ActionStates import ActionStates

class UpDateWindow(QMainWindow, UpdatePrice, SetPrice):

    def __init__(self):
        QMainWindow.__init__(self)
        # 设置为全局小部件
        self.ui = database()
        self.m_flag = False
        self.ui.setupUi(self)
        self.all_names = []
        self.update = UpdatePrice()
        self.setprice = SetPrice()
        global widgets
        widgets = self.ui
        global update
        update = self.update
        global set
        set = self.setprice

        widgets.pushButton_14.clicked.connect(self.close_app)
        widgets.pushButton_14.setToolTip('关闭🥰')
        widgets.pushButton_13.clicked.connect(self.showMinimized)
        widgets.pushButton_13.setToolTip('最小化🥰')
        widgets.pushButton_17.clicked.connect(self.roll_back)
        widgets.pushButton_17.setToolTip('返回🥰')
        my_regex = QtCore.QRegExp("[a-z0-9_]{16}")
        my_validator = QtGui.QRegExpValidator(my_regex, widgets.lineEdit)
        widgets.lineEdit.setValidator(my_validator)
        widgets.lineEdit.textEdited.connect(self.get_like_names_table_name)
        # widgets.lineEdit.textEdited.connect(self.work.start)
        widgets.lineEdit.setToolTip('搜索报价🥰')

    # def buttonClick(self):
    #     self.work = work_thread()
    #     self.work.start()
    # 搜索框模糊查询报价
    def get_like_names_table_name(self):

        try:
            for k in self.all_names:
                k.close()
            like_name = widgets.lineEdit.text()
            names = self.get_like_names_table(like_name)
            size = len(names)
            self.all_names = []
            widgets.textBrowser.setText(f"共{size}张表")
            widgets.textBrowser.setToolTip('报价总数🥰')
            # print('11')
            for p, j in zip(names, range(size + 1)):
                if p == "empty_unit":
                    name = "单价"
                else:
                    name = p[4:]
                p = QtWidgets.QPushButton(widgets.scrollArea)
                self.all_names.append(p)
                row = math.ceil((j + 1) / 15) - 1
                loc = ((j + 1) / 15 - row) * 15 - 1
                p.setGeometry(QtCore.QRect(25 + loc * 95, 30 + row * 80, 80, 80))
                p.setStyleSheet("QPushButton{\n"
                                "    background-color: rgba(255, 255, 255, 0);\n"
                                "    image: url(:/images/images/database_yy.png);\n"
                                "    font: 15pt;"
                                "}\n"
                                "QPushButton:hover{\n"
                                "    background-color: rgba(255, 255, 255, 0);\n"
                                "    image: url(:/images/images/database_blue.png);\n"
                                "    font: 22pt;"
                                "}\n"
                                "QPushButton:pressed{\n"
                                "    background-color: rgba(255, 255, 255, 0);\n"
                                "    image: url(:/images/images/database_blue.png);\n"
                                "    font: 18pt;"
                                "}")
                p.setText(f'{name}')
                p.setObjectName(str(p))
                p.setGraphicsEffect(
                    QtWidgets.QGraphicsDropShadowEffect(blurRadius=10, xOffset=3, yOffset=0))
                p.clicked.connect(lambda: print(p.sender().text()))
                self.set_tolltip(name, p)
                p.show()
            return True
        except Exception as ex:
            print(ex)


    def set_tolltip(self, name, p):
        has = self.if_nw_table(name)
        data_price = self.get_empty_unit_price_data(name)
        data_rows = data_price.shape[0]
        if has == 0:
            if data_rows == 1:
                empty_price = data_price['em_price'][0]
                unit_price = data_price['unit_price'][0]
                optioner = data_price['entering_clerk'][0]
                p.setToolTip("考虑西北比例🥰\n"
                             f"操作人:\t{optioner}\n"
                             f"空包价格:\t￥{empty_price}\n"
                             f"买单价格:\t￥{unit_price}")
            elif data_rows > 1:
                stores = data_price['emun_store']
                empty_prices = data_price['em_price']
                unit_prices = data_price['unit_price']
                optioners = data_price['entering_clerk']
                tip_text = "考虑西北比例🥰\n"
                for u in range(data_rows):
                    the_store = stores[u]
                    empty_price = empty_prices[u]
                    unit_price = unit_prices[u]
                    optioner = optioners[u]
                    tip_text += f"店铺:\t{the_store}\n" \
                                f"\t操作人:\t{optioner}\n" \
                                f"\t空包价格:\t￥{empty_price}\n" \
                                f"\t买单价格:\t￥{unit_price}\n"
                p.setToolTip(tip_text)

        else:
            if data_rows == 1:
                empty_price = data_price['em_price'][0]
                unit_price = data_price['unit_price'][0]
                optioner = data_price['entering_clerk'][0]
                p.setToolTip(f"不考虑西北比例🥰\n"
                             f"操作人:\t{optioner}\n"
                             f"空包价格:\t￥{empty_price}\n"
                             f"买单价格:\t￥{unit_price}")
            elif data_rows > 1:
                stores = data_price['emun_store']
                empty_prices = data_price['em_price']
                unit_prices = data_price['unit_price']
                optioners = data_price['entering_clerk']
                tip_text = "不考虑西北比例🥰\n"
                for u in range(data_rows):
                    the_store = stores[u]
                    empty_price = empty_prices[u]
                    unit_price = unit_prices[u]
                    optioner = optioners[u]
                    tip_text += f"店铺:\t{the_store}\n" \
                                f"\t操作人:\t{optioner}\n" \
                                f"\t空包价格:\t￥{empty_price}\n" \
                                f"\t买单价格:\t￥{unit_price}\n"
                p.setToolTip(tip_text)





    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(QtCore.Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if QtCore.Qt.LeftButton \
                and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(QtCore.Qt.ArrowCursor))


    def roll_back(self):
        for k in self.all_names:
            k.close()
        widgets.lineEdit.clear()
        self.close()
        Windows.SETOFFER_WIN.show()

    def close_app(self):
        self.logs_in(ActionStates.CLOSEAPPLICATION)
        self.close()


    def to_message(self, text):
        Windows.MESSAGE_WIN.set_message(text)





# # 创建子线程
# class work_thread(QThread,UpDateWindow):  # 继承:QThread
#
#
#     def __init__(self):
#         super().__init__()
#     # 重写run方法
#     def run(self):
#         print("11")
#         self.get_like_names_table_name()
#         # print('55')


